﻿using System;
using System.Linq;
using System.Data;
using Apps.IDAL;
using Apps.Models;
using System.Data.Entity;

namespace Apps.DAL
{
	public class SysSampleRepository : ISysSampleRepository, IDisposable
	{
		/// <summary>
		/// 获取列表
		/// </summary>
		/// <param name="db">数据库上下文</param>
		/// <returns>数据列表</returns>
		public IQueryable<SysSample> GetList(AppsDBEntities db)
		{
			IQueryable<SysSample> list = db.SysSample.AsQueryable();
			return list;
		}
		/// <summary>
		/// 创建一个实体
		/// </summary>
		/// <param name="entity"></param>
		/// <returns></returns>
		public int Create(SysSample entity)
		{
			using (AppsDBEntities db = new AppsDBEntities())
			{
				db.Set<SysSample>().Add(entity);
				return db.SaveChanges();
			}
		}
		/// <summary>
		/// 删除一个实体
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public int Delete(string id)
		{
			int Id = Convert.ToInt32(id);
			using (AppsDBEntities db = new AppsDBEntities())
			{
				SysSample sysSample = db.SysSample.SingleOrDefault(x => x.Id == Id);
				db.Set<SysSample>().Remove(sysSample);
				return db.SaveChanges();
			}
		}
		/// <summary>
		/// 修改一个实体
		/// </summary>
		/// <param name="entity">实体</param>
		/// <returns></returns>
		public int Edit(SysSample entity)
		{
			using (AppsDBEntities db = new AppsDBEntities())
			{
				db.Set<SysSample>().Attach(entity);
				db.Entry<SysSample>(entity).State = EntityState.Modified;
				return db.SaveChanges();
			}
		}
		/// <summary>
		/// 获取一个实体
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public SysSample GetById(string id)
		{
			int Id = Convert.ToInt32(id);
			using (AppsDBEntities db = new AppsDBEntities())
			{
				return db.SysSample.SingleOrDefault(x => x.Id == Id);
			}
		}
		/// <summary>
		/// 判断一个实体是否存在
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public bool IsExist(string id)
		{
			using (AppsDBEntities db = new AppsDBEntities())
			{
				SysSample sysSample = GetById(id);
				if (sysSample != null)
					return true;
				return false;
			}
		}

		public void Dispose()
		{
			throw new NotImplementedException();
		}
	}
}
